package junmo.s3caesar.mandu.nhkradionews.alert;

import java.util.Timer;
import java.util.TimerTask;

import junmo.s3caesar.mandu.nhkradionews.R;
import junmo.s3caesar.mandu.nhkradionews.activity.NewsListActivity;
import junmo.s3caesar.mandu.nhkradionews.data.NewsFinder;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.IBinder;
import android.util.Log;

public class NewsAlertService extends Service {
	
	private static final String CLASSTAG = NewsAlertService.class.getSimpleName();
	
	private SharedPreferences sharedPreferences;
	
	private String lastBuildDate;
	private NewsFinder finder;
	
	private NotificationManager notificationManager;
	private final int NOTIFICATION_ID = 1;

	private Timer timer;
	private TimerTask task = new TimerTask() {

        @Override
        public void run() {
        	Log.v(CLASSTAG, "TimerTask:run()");
        	
        	sharedPreferences = getSharedPreferences("PROPERTIES", Context.MODE_PRIVATE);
        	lastBuildDate = sharedPreferences.getString("KEY_LAST_BUILD_DATE", "N/A");
        	String findLastBuildDate = finder.findLastBuildDate();
        	Log.v(CLASSTAG, lastBuildDate + " > " + findLastBuildDate);

        	if(lastBuildDate.equals("N/A")) {
        		Editor sharedPreferencesEditor = sharedPreferences.edit();
        		sharedPreferencesEditor.putString("KEY_LAST_BUILD_DATE", findLastBuildDate);
        		sharedPreferencesEditor.commit();
        		
        	} else if(!lastBuildDate.equals(findLastBuildDate)) {
        		Editor sharedPreferencesEditor = sharedPreferences.edit();
        		sharedPreferencesEditor.putString("KEY_LAST_BUILD_DATE", findLastBuildDate);
        		sharedPreferencesEditor.commit();
        		
        		notificationManager.notify(NOTIFICATION_ID, notification());
        	}
        }
        
    };

	@Override
	public void onCreate() {
		Log.v(CLASSTAG, "onCreate()");
		finder = new NewsFinder("http://www.nhk.or.jp/r-news/podcast/nhkradionews.xml");
		notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		this.timer = new Timer();
		this.timer.schedule(this.task, 300000, 1800000); // 5분 후 시작, 30분 마다
//		this.timer.schedule(this.task, 60000, 60000); // 테스트용 : 1분 후 시작, 1분 마다
	}
	
	@Override
	public void onDestroy() {
		Log.v(CLASSTAG, "onDestroy()");
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		Log.v(CLASSTAG, "onBind()");
		return null;
	}
	
	private Notification notification() {
		Log.v(CLASSTAG, "notification()");
		
		int icon = R.drawable.ic_launcher;
		CharSequence tickerText = "새로운 뉴스";
		long when = System.currentTimeMillis();
		Notification notification = new Notification(icon, tickerText, when);
		
		Context context = getApplicationContext();
		CharSequence contentTitle = "NHK라디오뉴스";
		CharSequence contentText = "새로운 뉴스, 듣고! 찾고!";
		Intent notificationIntent = new Intent(this, NewsListActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		
		return notification;
	}
	
}
